﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.ComponentModel;
using System.Reflection;
using PopupBehaviorProto;
using SilverFlow.Controls;
using Microsoft.Expression.Interactivity.Core;
using SilverlightDebateView;
using SilverlightDebateView.Classes;

namespace PopupBehaviorProto
{
    /// <summary>
    /// Prototype ... hisowa
    /// </summary>
    [System.ComponentModel.Description("Launches a Popup with custom UI on Event Trigger")]
    public class PopUpProtoV2 : TargetedTriggerAction<FloatingWindowHost>, INotifyPropertyChanged
    {
        FloatingWindowHost objFloatingWindowHost;
        private double xpos = 50;
        private double ypos = 50;
        private Point startPoint;

        //constructor
        public PopUpProtoV2()
        {
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            objFloatingWindowHost = (FloatingWindowHost)(this.AssociatedObject);
        }

        #region propertyname

        public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register("PropertyName", typeof(string), typeof(PopUpProtoV2), null);
        public string PropertyName
        {
            get
            {
                return (string)base.GetValue(PropertyNameProperty);
            }
            set
            {
                base.SetValue(PropertyNameProperty, value);
            }
        }

        public static readonly DependencyProperty CustomChildWindowProperty = DependencyProperty.Register("CustomChildWindow", typeof(FloatingWindow), typeof(PopUpProtoV2), null);
        public FloatingWindow CustomChildWindow
        {
            get
            {
                return (FloatingWindow)base.GetValue(CustomChildWindowProperty);
            }
            set
            {
                base.SetValue(CustomChildWindowProperty, value);
            }
        }

        #endregion

        FloatingWindow ChildPopUp;

        #region ReturnICommandProperty

        public static readonly DependencyProperty ReturnICommandProperty = DependencyProperty.Register("ReturnICommand", typeof(ICommand), typeof(PopUpProtoV2), null);

        public ICommand ReturnICommand
        {
            get
            {
                return (ICommand)base.GetValue(ReturnICommandProperty);
            }
            set
            {
                base.SetValue(ReturnICommandProperty, value);
            }
        }
        #endregion
        
        // Dependancy Property

        public static readonly DependencyProperty HostMainViewModelProperty = DependencyProperty.Register("HostMainViewModel", typeof(SilverlightDebateViewModel.MainPageViewModel), typeof(PopUpProtoV2), null);

        public SilverlightDebateViewModel.MainPageViewModel HostMainViewModel
        {
            get
            {
                return (SilverlightDebateViewModel.MainPageViewModel)base.GetValue(HostMainViewModelProperty);
            }
            set
            {
                base.SetValue(HostMainViewModelProperty, value);
            }
        }

        #region Invoke

        //Shows popup on event trigger you set in designer
        protected override void Invoke(object parameter)
        {
            if (HostMainViewModel.ReturnICommandNewOpinion != null)
            {
                ChildPopUp = (FloatingWindow)Activator.CreateInstance(CustomChildWindow.GetType());

                objFloatingWindowHost.Add(ChildPopUp);

                Point startPoint = new Point(50, 50);

                ChildPopUp.Closing += new EventHandler<CancelEventArgs>(ChildPopUp_Closing);

                // Show Window
                ChildPopUp.Show(startPoint);

                // Bring the window to the front
                BringWindowToFront(ChildPopUp);
            }
        }

        #endregion

        #region Events

        void ChildPopUp_Closing(object sender, CancelEventArgs e)
        {
            IPopup pop = ChildPopUp as IPopup;

            if (ReturnICommand != null && pop != null && pop.DialogResult.HasValue && pop.DialogResult.Value)
            {
                Panel pnl = ChildPopUp.GetType().GetProperty("Content").GetValue(ChildPopUp, null) as Panel;

                if (pnl != null)
                {
                    object dc = ((FrameworkElement)pnl.Children[0]).DataContext;

                    //return the data property
                    ReturnICommand.Execute(dc.GetType().GetProperty(PropertyName).GetValue(dc, null));
                }
            }

            //reset
            HostMainViewModel.ReturnICommandNewOpinion = null;

            RemoveHandlers();
        }

        private void RemoveHandlers()
        {
            ChildPopUp.Closing -= new EventHandler<CancelEventArgs>(ChildPopUp_Closing);
        }

        #endregion

        // Utility

        #region BringWindowToFront
        private void BringWindowToFront(FloatingWindow popup)
        {
            int HighestZIndex = 0;
            foreach (FrameworkElement fWindow in objFloatingWindowHost.Children)
            {
                int zValue = ((int)fWindow.GetValue(Canvas.ZIndexProperty));
                if (zValue > HighestZIndex)
                {
                    HighestZIndex = zValue + 1;
                }

                fWindow.SetValue(Canvas.ZIndexProperty, zValue);
                objFloatingWindowHost.UpdateLayout();
            }

            popup.SetValue(Canvas.ZIndexProperty, HighestZIndex++);
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}